﻿using System;
using System.Windows.Forms;

namespace InputHandlers.KeyboardHandling
{
    /// <summary>
    /// Create new <see cref="ToggleKey"/> objects.
    /// </summary>
    public static class ToggleKeyFactory
    {
        /// <summary>
        /// Decide if a given key combination has an extended key in it.
        /// </summary>
        public static bool HasExtendedKey(Keys keyCombination)
        {
            return
                ((keyCombination & Keys.Alt) == Keys.Alt) ||
                ((keyCombination & Keys.Control) == Keys.Control) ||
                ((keyCombination & Keys.Shift) == Keys.Shift);
        }

        /// <summary>
        /// Decide if a given key is an extended key.
        /// </summary>
        /// <param name="key">The key to check.</param>
        /// <returns>Whether the given key is an extended one.</returns>
        public static bool IsExtendedKey(Keys key)
        {
            return key == Keys.Alt || key == Keys.Control || key == Keys.Shift;
        }

        /// <summary>
        /// Creates a <see cref="ToggleKey"/> object, according to the given key-definition.
        /// </summary>
        /// <returns>The new created toggle key.</returns>
        /// <param name="keyDefinition">Key definition.</param>
        /// <param name="operationToTake">An operation to take when requesting to verify the validity of an input against this key-definition.</param>
        /// <param name="isOn">Whether the key is started as toggled on.</param>
        public static ToggleKey CreateToggleKey(Keys keyDefinition, Action operationToTake, bool isOn = false)
        {
            if (IsExtendedKey(keyDefinition))
            {
                return new ExtendedToggleKey(keyDefinition, operationToTake, isOn);
            }

            return new ToggleKey(keyDefinition, operationToTake, isOn);
        }
    }
}
